﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DAL;
using BLL;

namespace QLTTTH
{
    public partial class frmDiemThi : DevExpress.XtraEditors.XtraForm
    {
        public frmDiemThi()
        {
            InitializeComponent();
        }

        private void frmDiemThi_Load(object sender, EventArgs e)
        {
            var dsmh = KhoaHocManager.LayDanhSachKhoaHoc();
            if (dsmh.Success)
            {
                loupMaKH.Properties.DataSource = dsmh.Result;
                loupMaKH.Properties.ValueMember = "MaKH";
                loupMaKH.Properties.DisplayMember = "TenKH";

            }
            var dsHV = HocVienManager.LayDanhSachHocVien();
            if (dsHV.Success)
            {
                loupMaHV.Properties.DataSource = dsHV.Result;
                loupMaHV.Properties.ValueMember = "MaHV";
                loupMaHV.Properties.DisplayMember = "HoTen";
            }
            var dsKT = KyThiManager.LayDanhSachKyThi();
            if (dsKT.Success)
            {
                loupMaKT.Properties.DataSource = dsKT.Result;
                loupMaKT.Properties.ValueMember = "MaKyThi";
                loupMaKT.Properties.DisplayMember = "NgayThi";
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        public void FillControl(DiemThi dt)
        {
            this.txtDiemLT.Text = dt.DiemLyThuyet.ToString();
            this.txtDiemTH.Text = dt.DiemThucHanh.ToString();
            this.loupMaHV.EditValue = dt.MaHV;
            this.loupMaKH.EditValue = dt.MaKH;
            this.loupMaKT.EditValue = dt.MaKyThi;
            this.txtKQ.Text = dt.KetQua;
        }


        public delegate void SendData(DiemThi diemthi);

        public SendData sendData;

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (sendData != null)
            {
                var vResult = new DiemThiManager().Validate(LayDiemThi());
                if (vResult.Valid)
                {
                    sendData(LayDiemThi());
                    DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    XtraMessageBox.Show(vResult.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        public DiemThi LayDiemThi()
        {
            DiemThi diemthi = new DiemThi();

            if (!loupMaKH.EditValue.ToString().Equals(""))
            {
                diemthi.MaKH = int.Parse(loupMaKH.EditValue.ToString());
            }
            diemthi.MaHV = int.Parse(loupMaHV.EditValue.ToString());
            diemthi.MaKyThi = int.Parse(loupMaKT.EditValue.ToString());
            diemthi.DiemLyThuyet = float.Parse(txtDiemLT.Text.ToString());
            diemthi.DiemThucHanh = float.Parse(txtDiemTH.Text.ToString());
            diemthi.KetQua = txtKQ.Text;
            return diemthi;
        }


    }
}